import http.client,unittest
class Woniu(unittest.TestCase):
    def test_login_add(self):
        #请求连接
        con = http.client.HTTPConnection(host="192.168.8.143",port="8080")
        #发送post请求
        con.request(method="POST",url="/WoniuSales-20180508-V1.4-bin/user/login"
                    ,body="username=admin&password=admin123&verifycode=0000",
                    headers={"Content-Type":" application/x-www-form-urlencoded","charset":"UTF-8"}
                    )
        #接收cookie
        res = con.getresponse().getheader("Set-Cookie")
        cook = res.split(";")[0]
        print(cook)
        ##会员管理新增
        #建立连接
        con = http.client.HTTPConnection(host="192.168.8.143",port="8080")
        con.request(method="POST",url="/WoniuSales-20180508-V1.4-bin/customer/add",
                    body="customername=baby&customerphone=184082201252&childsex=男&childdate=2018-11-10&creditkids=1&creditcloth=11".encode(),
                    headers={"Content-Type":" application/x-www-form-urlencoded","charset":"UTF-8","Cookie":cook}
                    )
        res = con.getresponse().read().decode()
        print(res)
        #断言
        self.assertAlmostEqual(res,"add-successful",msg="代码没问题，但是新增失败！")
        print("新增成功！")


if __name__ == "__main__":
    Woniu().test_login_add()